#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>

using namespace std;

static void sep(const string &title) {
	cout << "\n=== " << title << " ===" << endl;
}

// 1) 经典下标 for（读/写均可）
void test_index_for() {
	sep("1) index for");
	string s = "Hello World!";
	for (size_t i = 0; i < s.size(); ++i) {
		char c = s[i];
		cout << c << ' ';
	}
	cout << endl;
	for (size_t i = 0; i < s.size(); ++i) {
		s[i] = std::toupper(static_cast<unsigned char>(s[i]));
	}
	cout << s << endl;
}

// 2) 范围 for（按值/按引用）
void test_range_for() {
	sep("2) range for");
	string s = "Hello World!";
	for (char c : s) cout << c << ' ';
	cout << endl;
	for (char &c : s) c = std::tolower(static_cast<unsigned char>(c));
	cout << s << endl;
}

// 3) 迭代器遍历（正向/反向）
void test_iterators() {
	sep("3) iterators");
	string s = "Hello";
	for (auto it = s.begin(); it != s.end(); ++it) cout << *it << ' ';
	cout << endl;
	for (auto it = s.rbegin(); it != s.rend(); ++it) cout << *it << ' ';
	cout << endl;
}

// 4) 指针遍历（c_str / data）
void test_pointers() {
	sep("4) pointers");
	string s = "Pointer";
	const char *p = s.c_str();
	for (size_t i = 0; i < s.size(); ++i) cout << p[i] << ' ';
	cout << endl;
}

// 5) std::for_each 与 Lambda
void test_for_each_lambda() {
	sep("5) for_each + lambda");
	string s = "Lambda";
	std::for_each(s.begin(), s.end(), [](char &c){ c = std::toupper(static_cast<unsigned char>(c)); });
	cout << s << endl;
}

// 6) 索引步进/条件跳过
void test_step_skip() {
	sep("6) step & skip");
	string s = "a b\tc\nd";
	for (size_t i = 0; i < s.size(); ) {
		unsigned char c = static_cast<unsigned char>(s[i]);
		if (std::isspace(c)) { ++i; continue; }
		cout << s[i] << ' ';
		++i;
	}
	cout << endl;
}

// 7) 频次统计（ASCII）
void test_freq_ascii() {
	sep("7) freq ascii");
	string s = "abracadabra";
	int cnt[256] = {0};
	for (unsigned char c : s) cnt[c]++;
	cout << "count('a')=" << cnt[static_cast<unsigned char>('a')] << endl;
}

// 8) 查找与遍历结合
void test_find_loop() {
	sep("8) find loop");
	string s = "index of x in text: xXx";
	for (size_t pos = 0; (pos = s.find('x', pos)) != string::npos; ++pos) {
		cout << "found at " << pos << endl;
	}
}

// 9) string_view（只读）
void test_string_view() {
	sep("9) string_view");
	string s = "read only";
    // 在 C++11 下无 string_view，这里用 const string& 模拟只读遍历
    const string &sv = s;
    for (char c : sv) cout << c << ' ';
	cout << endl;
}

// 10) UTF-8 字节遍历（示意：按字节）
void test_utf8_bytes() {
	sep("10) utf-8 bytes (demo)");
	// 注意：这按字节遍历，中文/emoji 会被拆分
	string s = u8"中文🙂";
	for (unsigned char b : s) cout << std::hex << (int)b << ' ';
	cout << std::dec << endl;
}

// 11) 反转与遍历
void test_reverse_traverse() {
	sep("11) reverse + traverse");
	string s = "reverse";
	std::reverse(s.begin(), s.end());
	for (char c : s) cout << c;
	cout << endl;
}

// 12) 性能/安全要点演示（toupper 安全用法）
void test_safety_perf() {
	sep("12) safety/perf");
	string s = "MiXeD";
	for (char &c : s) c = std::toupper(static_cast<unsigned char>(c));
	cout << s << endl;
}

int main() {
	test_index_for();
	test_range_for();
	test_iterators();
	test_pointers();
	test_for_each_lambda();
	test_step_skip();
	test_freq_ascii();
	test_find_loop();
	test_string_view();
	test_utf8_bytes();
	test_reverse_traverse();
	test_safety_perf();
	return 0;
}
